package com.stu.exercise.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.exercise.entity.R;
import com.stu.exercise.entity.Shopping;
import com.stu.exercise.mapper.ShopMapper;
import com.stu.exercise.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shopping> implements ShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Override
    public R<List<Shopping>> getListShop(String name) {
        List<Shopping> list = shopMapper.selectShops(name);
        return R.success(list);
    }

    @Override
    public R batchStorage(List<Integer> ids) {
        LambdaUpdateWrapper<Shopping> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Shopping::getStatus, 1).in(Shopping::getId, ids);
        List<Shopping> shops = shopMapper.selectBatchIds(ids);
        int stg = shopMapper.batchStorage(shops);
        shopMapper.update(wrapper);
        if (stg == ids.size()){
            return R.success("入库成功", null);
        }
        return R.error(500, "入库失败");
    }

}
